In [1]:
!which python
/Users/shubhang/Documents/Course/pgp-aiml-oct-21/FMT-Project/FMT-Project/venv/bin/python
In [2]:
!pip install numpy
Requirement already satisfied: numpy in ./venv/lib/python3.8/site-packages (1.22.3)
WARNING: You are using pip version 21.3.1; however, version 22.0.4 is available.
You should consider upgrading via the '/Users/shubhang/Documents/Course/pgp-aiml-oct-21/FMT-Project/FMT-Project/venv/bin/python -m pip install --upgrade pip' command.
In [3]:
!pip install matplotlib
Requirement already satisfied: matplotlib in ./venv/lib/python3.8/site-packages (3.5.1)
Requirement already satisfied: python-dateutil>=2.7 in ./venv/lib/python3.8/site-packages (from matplotlib) (2.8.2)
Requirement already satisfied: pyparsing>=2.2.1 in ./venv/lib/python3.8/site-packages (from matplotlib) (3.0.7)
Requirement already satisfied: cycler>=0.10 in ./venv/lib/python3.8/site-packages (from matplotlib) (0.11.0)
Requirement already satisfied: packaging>=20.0 in ./venv/lib/python3.8/site-packages (from matplotlib) (21.3)
Requirement already satisfied: numpy>=1.17 in ./venv/lib/python3.8/site-packages (from matplotlib) (1.22.3)
Requirement already satisfied: pillow>=6.2.0 in ./venv/lib/python3.8/site-packages (from matplotlib) (9.1.0)
Requirement already satisfied: kiwisolver>=1.0.1 in ./venv/lib/python3.8/site-packages (from matplotlib) (1.4.2)
Requirement already satisfied: fonttools>=4.22.0 in ./venv/lib/python3.8/site-packages (from matplotlib) (4.31.2)
Requirement already satisfied: six>=1.5 in ./venv/lib/python3.8/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
WARNING: You are using pip version 21.3.1; however, version 22.0.4 is available.
You should consider upgrading via the '/Users/shubhang/Documents/Course/pgp-aiml-oct-21/FMT-Project/FMT-Project/venv/bin/python -m pip install --upgrade pip' command.
In [4]:
from matplotlib import pyplot as plt
import numpy as np

Read all images and keep displaying them¶

In [5]:
story_img_1 = plt.imread('Experiment/1.jpeg')
In [6]:
type(story_img_1)
Out[6]:
numpy.ndarray
In [7]:
story_img_1.shape
Out[7]:
(300, 600, 3)
In [8]:
plt.imshow(story_img_1)
Out[8]:
<matplotlib.image.AxesImage at 0x1203e5160>
In [9]:
story_imgs = []
In [10]:
import glob
In [11]:
story_imgs = glob.glob('Experiment/*')
In [12]:
story_imgs
Out[12]:
['Experiment/93.1.jpeg',
 'Experiment/1.jpeg',
 'Experiment/9.jpg',
 'Experiment/98.jpeg',
 'Experiment/94.jpeg',
 'Experiment/97.gif',
 'Experiment/6.jpeg',
 'Experiment/7.jpeg',
 'Experiment/99.jpeg',
 'Experiment/8.jpeg',
 'Experiment/4.jpeg',
 'Experiment/93.0.jpg',
 'Experiment/5.jpeg',
 'Experiment/95.jpg',
 'Experiment/2.jpeg',
 'Experiment/92.jpg',
 'Experiment/91.jpeg',
 'Experiment/3.jpeg']
In [13]:
story_img_arr = []
In [14]:
story_imgs.sort()
In [15]:
story_imgs
Out[15]:
['Experiment/1.jpeg',
 'Experiment/2.jpeg',
 'Experiment/3.jpeg',
 'Experiment/4.jpeg',
 'Experiment/5.jpeg',
 'Experiment/6.jpeg',
 'Experiment/7.jpeg',
 'Experiment/8.jpeg',
 'Experiment/9.jpg',
 'Experiment/91.jpeg',
 'Experiment/92.jpg',
 'Experiment/93.0.jpg',
 'Experiment/93.1.jpeg',
 'Experiment/94.jpeg',
 'Experiment/95.jpg',
 'Experiment/97.gif',
 'Experiment/98.jpeg',
 'Experiment/99.jpeg']
In [16]:
len(story_imgs)
Out[16]:
18
In [17]:
rows = 3
columns = 6
w = 10
h = 10
In [18]:
for idx, image_path in enumerate(story_imgs):
    img = plt.imread(image_path)
    story_img_arr.append(img)
In [19]:
fig = plt.figure(figsize=(10, 20))
columns = 3
rows = 6
for i in range(1, columns*rows +1):
    img = story_img_arr[i-1]
    fig.add_subplot(rows, columns, i)
    plt.title(story_imgs[i-1])
    plt.imshow(img)
plt.show()
In [137]:
dialogues = [
    "Thor use to leave his hammer and helmet anywhere, he knows no one is worthy to pick up",
    "Ash's Pickachu notices it one day",
    "He had never seen such a fancy and shiny thing, and became happy",
    "Unknowingly he picked them up and started dancing",
    "Thor came to the place and didn't like that an unknown creature is worthy!!",
    "Give back my Mjolnir!! It's not a toy!",
    "Pickachu didn't like this and became angry",
    "Steve: Seems this strange creature is going to attack!",
    "Pickachu attacks with thunderbolt",
    "Thor: THANK YOU, SWEET RABBIT. I'm not in a gaming mood!!",
    "Thor: You’re spark is big. I’ve fought bigger.",
    "Both collides, Boom!!",
    "Pickachu teases with funny faces",
    "I like this small creature, he's brave and courageous",
    "Pickachu on hearing these compliments also gives up anger"
]
Looking at the above images seems we need to perform some cropping¶

Lets see the image 2¶

In [21]:
img2 = story_img_arr[1]
plt.imshow(img2)
Out[21]:
<matplotlib.image.AxesImage at 0x120aded30>
In [22]:
img2.shape
Out[22]:
(477, 636, 3)
In [23]:
def crop(arr, width_start, width_end, height_start, height_end):
    result = arr[height_start:height_end, width_start:width_end, :]
    return result
In [24]:
plt.imshow(crop(img2, 0, 430, 0, 477))
Out[24]:
<matplotlib.image.AxesImage at 0x120b46820>

Observation seems our cropping is working as expected, lets check complex crop on one image¶

In [25]:
temp_img = story_img_arr[14]
plt.imshow(temp_img)
Out[25]:
<matplotlib.image.AxesImage at 0x120ba8f40>
In [26]:
temp_img.shape
Out[26]:
(349, 620, 3)
In [27]:
plt.imshow(crop(temp_img, 200, 450, 240, 330))
Out[27]:
<matplotlib.image.AxesImage at 0x120c1c100>
Lets create one text area function¶
In [28]:
def add_whitespace_area(arr, height, at_bottom=True, add_white = True):
    width = arr.shape[1]
    area = np.zeros(shape=(height, width, 3), dtype=int)
    white_space = 255-area if add_white else area
    return np.vstack((arr, white_space)) if at_bottom else np.vstack((white_space, arr))
In [29]:
plt.imshow(add_whitespace_area(img2, 100, False))
Out[29]:
<matplotlib.image.AxesImage at 0x120c733d0>
In [30]:
plt.imshow(add_whitespace_area(img2, 100))
Out[30]:
<matplotlib.image.AxesImage at 0x120cb9f10>
Seems we are able to add the whitespace area below and above the images¶
In [31]:
from PIL import Image, ImageFont, ImageDraw
In [32]:
sample_img = add_whitespace_area(img2, 100)
plt.imshow(sample_img)
Out[32]:
<matplotlib.image.AxesImage at 0x1212400a0>
In [35]:
from matplotlib import cm
im = Image.fromarray(np.uint8(sample_img))
In [36]:
fontsize = 20
font = ImageFont.truetype(font='./twinkle-star/TwinkleStar-Regular.ttf', size=40)
In [37]:
draw = ImageDraw.Draw(im)
draw.text((0,500), "This is a test", (0,0,0), font=font)
plt.imshow(im)
Out[37]:
<matplotlib.image.AxesImage at 0x12044efa0>
In [38]:
im.show()
In [39]:
draw.text?
In [40]:
def write_text(arr, width, height, text, font_size):
    changed_image = arr.copy()
    im = Image.fromarray(np.uint8(changed_image))
    draw = ImageDraw.Draw(im)
    font = ImageFont.truetype(font='./twinkle-star/TwinkleStar-Regular.ttf', size=font_size)
    draw.text((width,height), text, (0,0,0), font=font)
    return im
In [41]:
plt.imshow(write_text(sample_img, 10, 500, dialogues[1], 50))
Out[41]:
<matplotlib.image.AxesImage at 0x121e3c6d0>
In [42]:
dialogues[1]
Out[42]:
"Ash's Pickachu notices it"
In [43]:
len(dialogues)
Out[43]:
15

Let's check flipping of images¶

In [44]:
attack_middle = crop(temp_img, 200, 450, 240, 330)
plt.imshow(attack_middle)
Out[44]:
<matplotlib.image.AxesImage at 0x121e9eaf0>
In [45]:
attack_middle.shape
Out[45]:
(90, 250, 3)
We have an inbuilt function¶
In [46]:
plt.imshow(np.fliplr(attack_middle))
Out[46]:
<matplotlib.image.AxesImage at 0x121f0b7c0>

But what is the fun in that??¶

In [47]:
img_flip = attack_middle[:, ::-1 , :]
plt.imshow(img_flip)
Out[47]:
<matplotlib.image.AxesImage at 0x121f6e970>
In [48]:
plt.imshow(attack_middle)
Out[48]:
<matplotlib.image.AxesImage at 0x121fd3580>

Seems we are able to flip in 1 line as well¶

In [49]:
def concatenate_images(array_images):
    return np.hstack(array_images)
In [51]:
story_img_arr[12].shape
Out[51]:
(249, 602, 3)
In [52]:
img_flip.shape
Out[52]:
(90, 250, 3)
In [53]:
story_img_arr[13].shape
Out[53]:
(168, 299, 3)
In [54]:
merged_area = add_whitespace_area(add_whitespace_area(img_flip, 
                                                      79, 
                                                      add_white=False), 
                                  80, 
                                  at_bottom=False, 
                                  add_white=False)
In [55]:
merged_area.shape
Out[55]:
(249, 250, 3)
In [56]:
right_area = add_whitespace_area(add_whitespace_area(story_img_arr[13], 
                                                      40, 
                                                      add_white=False), 
                                  41, 
                                  at_bottom=False, 
                                  add_white=False)
In [57]:
plt.imshow(right_area)
Out[57]:
<matplotlib.image.AxesImage at 0x12220ac40>
In [58]:
plt.imshow(concatenate_images([story_img_arr[12], merged_area, right_area]))
Out[58]:
<matplotlib.image.AxesImage at 0x1222509a0>
In [59]:
plt.imshow(concatenate_images([story_img_arr[12], right_area]))
Out[59]:
<matplotlib.image.AxesImage at 0x1222ab850>
In [60]:
plt.imshow(np.fliplr(story_img_arr[9]))
Out[60]:
<matplotlib.image.AxesImage at 0x12230a220>
In [61]:
pick_attack = np.fliplr(story_img_arr[9])
plt.imshow(pick_attack)
Out[61]:
<matplotlib.image.AxesImage at 0x12236be20>
In [62]:
pick_attack.shape
Out[62]:
(163, 309, 3)
In [63]:
plt.imshow(story_img_arr[12])
Out[63]:
<matplotlib.image.AxesImage at 0x1223d3370>
In [64]:
story_img_arr[12].shape
Out[64]:
(249, 602, 3)
In [65]:
right_area = add_whitespace_area(add_whitespace_area(pick_attack, 
                                                      43, 
                                                      add_white=False), 
                                  43, 
                                  at_bottom=False, 
                                  add_white=False)
In [66]:
plt.imshow(concatenate_images([story_img_arr[12], right_area]))
Out[66]:
<matplotlib.image.AxesImage at 0x12242bee0>
In [67]:
plt.imshow(story_img_arr[11])
Out[67]:
<matplotlib.image.AxesImage at 0x122492310>
In [68]:
story_img_arr[11].shape
Out[68]:
(163, 390, 3)
In [85]:
height_start = 30
height_end = 193
width_start = 375
width_end = 600
middle_attack = crop(story_img_arr[12], width_start, width_end, height_start, height_end)
In [86]:
plt.imshow(middle_attack)
Out[86]:
<matplotlib.image.AxesImage at 0x1240a9850>
In [71]:
middle_attack.shape
Out[71]:
(163, 220, 3)
In [87]:
plt.imshow(concatenate_images([story_img_arr[11], middle_attack, pick_attack]))
Out[87]:
<matplotlib.image.AxesImage at 0x1243e2cd0>
In [88]:
both_attack = concatenate_images([story_img_arr[11], middle_attack, pick_attack])
In [90]:
plt.imshow(add_whitespace_area(both_attack, 100))
Out[90]:
<matplotlib.image.AxesImage at 0x1236a6ac0>
In [106]:
both_attack_with_caption = add_whitespace_area(both_attack, 130)
plt.imshow(both_attack_with_caption)
Out[106]:
<matplotlib.image.AxesImage at 0x123b03f10>
In [111]:
plt.imshow(write_text(both_attack_with_caption, 
                      0, 140, 
                      """
                      Pickachu attacks with thunderbolt, 
                      Thor Says: Thank you, Sweet RABBIT. 
                              I'm not in a gaming mood!!"""
                      , 30))
Out[111]:
<matplotlib.image.AxesImage at 0x1245b9f10>
In [92]:
dialogues
Out[92]:
['Thor use to leave his hammer and helmet anywhere, he knows no one is worthy to pick up',
 "Ash's Pickachu notices it",
 'He had never seen such a fancy and shiny thing, and became happy',
 'Unknowingly he picked them up and started dancing',
 "Thor came to the place and didn't like that an unknown creature is worthy!!",
 "Give back my Mjolnir!! It's not a toy!",
 "Pickachu didn't like this and became angry",
 'Steve: Seems this strange creature is going to attack!',
 'Pickachu attacks with thunderbolt',
 "Thor: THANK YOU, SWEET RABBIT. I'm not in a gaming mood!!",
 'Thor: You’re spark is big. I’ve fought bigger.',
 'Both collides, Boom!!',
 'Pickachu teases with funny faces',
 "I like this small creature, he's brave and courageous",
 'Pickachu on hearing these compliments also gives up anger']
In [112]:
attack_scene = write_text(both_attack_with_caption, 
                      0, 140, 
                      """
                      Pickachu attacks with thunderbolt, 
                      Thor Says: Thank you, Sweet RABBIT. 
                              I'm not in a gaming mood!!"""
                      , 30)
In [119]:
plt.imshow(story_img_arr[0])
Out[119]:
<matplotlib.image.AxesImage at 0x124828940>
In [114]:
story_img_arr[0].shape
Out[114]:
(300, 600, 3)
In [134]:
scene_1 = add_whitespace_area(story_img_arr[0], 130)
plt.imshow(scene_1)
Out[134]:
<matplotlib.image.AxesImage at 0x125090580>
In [135]:
scene_1 = write_text(scene_1, 
                      10, 300, 
                      """Mighty Thor use to leave his hammer and 
 helmet anywhere, he knows no one 
 is worthy to pick it up!!
                      """
                      , 30)
plt.imshow(scene_1)
Out[135]:
<matplotlib.image.AxesImage at 0x1250fa790>
In [148]:
scene_1.save("out/scene1.jpg")
In [139]:
plt.imshow(write_text(sample_img, 10, 500, dialogues[1], 40))
Out[139]:
<matplotlib.image.AxesImage at 0x1252335b0>
In [150]:
scene_2 = write_text(sample_img, 10, 500, dialogues[1], 40)
In [151]:
scene_2.save('out/scene_2.jpg')
In [162]:
scene_3 = add_whitespace_area(story_img_arr[2], 120)
scene_3 = write_text(scene_3, 
                      10, 250, 
                      """He had never seen such 
 a  fancy and shiny thing, 
 and became happy"""
                      , 20)
plt.imshow(scene_3)
Out[162]:
<matplotlib.image.AxesImage at 0x128f48280>
In [163]:
scene_3.save('out/scene_3.jpg')
In [173]:
scene_4 = add_whitespace_area(story_img_arr[3], 80)
scene_4 = write_text(scene_4, 
                      50, 310, 
                      """Unknowingly he picked them up 
        and started dancing"""
                      , 20)
plt.imshow(scene_4)
Out[173]:
<matplotlib.image.AxesImage at 0x1293142b0>
In [174]:
scene_4.save('out/scene4.jpg')
In [175]:
plt.imshow(story_img_arr[4])
Out[175]:
<matplotlib.image.AxesImage at 0x129367520>
In [176]:
story_img_arr[4].shape
Out[176]:
(415, 625, 3)
In [177]:
plt.imshow(crop(story_img_arr[4], 120, 600, 0, 415))
Out[177]:
<matplotlib.image.AxesImage at 0x129353070>
In [186]:
scene_5 = crop(story_img_arr[4], 120, 600, 0, 415)
scene_5 = add_whitespace_area(scene_5, 120)
scene_5 = write_text(scene_5, 
                      40, 415, 
                      """Thor came to the place and 
 didn't like that an 
 unknown creature is worthy!!"""
                      , 30)
plt.imshow(scene_5)
scene_5.save('out/scene5.jpg')
In [187]:
plt.imshow(story_img_arr[5])
Out[187]:
<matplotlib.image.AxesImage at 0x129741730>
In [188]:
story_img_arr[5].shape
Out[188]:
(143, 352, 3)
In [219]:
scene_6 = crop(story_img_arr[5], 150, 370, 0, 143)
scene_6 = add_whitespace_area(scene_6, 30)
scene_6 = write_text(scene_6, 
                      5, 143, 
                      """Give back my Mjolnir!!"""
                      , 20)
plt.imshow(scene_6)
scene_6.save('out/scene6.jpg')
In [209]:
plt.imshow(story_img_arr[6])
Out[209]:
<matplotlib.image.AxesImage at 0x129fd2a60>
In [212]:
story_img_arr[6].shape
Out[212]:
(422, 767, 3)
In [220]:
scene_7 = crop(story_img_arr[6], 150, 700, 0, 422)
scene_7 = add_whitespace_area(scene_7, 60)
scene_7 = write_text(scene_7, 
                      5, 422, 
                      """It's not a toy! Strange creature!!"""
                      , 40)
plt.imshow(scene_7)
scene_7.save('out/scene7.jpg')
In [221]:
plt.imshow(story_img_arr[7])
Out[221]:
<matplotlib.image.AxesImage at 0x12a4f9160>
In [222]:
story_img_arr[7].shape
Out[222]:
(1622, 2880, 3)
In [231]:
scene_8 = add_whitespace_area(story_img_arr[7], 200)
scene_8 = write_text(scene_8, 
                      5, 1622, 
                      """Pickachu didn't like this and became angry!!"""
                      , 150)
plt.imshow(scene_8)
scene_8.save('out/scene_8.jpg')
In [232]:
plt.imshow(story_img_arr[8])
Out[232]:
<matplotlib.image.AxesImage at 0x12a0f5ca0>
In [233]:
story_img_arr[8].shape
Out[233]:
(630, 1200, 3)
In [240]:
scene_9 = add_whitespace_area(story_img_arr[8], 150)
scene_9 = write_text(scene_9, 
                      50, 630, 
                      """Steve say's watch out!!
Seems this strange creature is going to attack!"""
                      , 55)
plt.imshow(scene_9)
scene_9.save('out/scene_9.jpg')
In [ ]:
# Run back home, little princess.
# "I've got this completely under control!"
In [243]:
plt.imshow(story_img_arr[10])
Out[243]:
<matplotlib.image.AxesImage at 0x12a4469a0>
In [245]:
story_img_arr[10].shape
Out[245]:
(1080, 1920, 3)
In [248]:
scene_10 = add_whitespace_area(story_img_arr[10], 150)
scene_10 = write_text(scene_10, 
                      50, 1080, 
                      """I've got this completely under control!"""
                      , 100)
plt.imshow(scene_10)
scene_10.save('out/scene10.jpg')
In [249]:
plt.imshow(attack_scene)
Out[249]:
<matplotlib.image.AxesImage at 0x12a36bbb0>
In [290]:
attack_scene.save('out/scene11.jpg')
In [261]:
pickachu_teasing1 = plt.imread('Experiment/97.1.jpg')
plt.imshow(pickachu_teasing1)
Out[261]:
<matplotlib.image.AxesImage at 0x129c45310>
In [262]:
pickachu_teasing2 = plt.imread('Experiment/97.2.jpg')
plt.imshow(pickachu_teasing2)
Out[262]:
<matplotlib.image.AxesImage at 0x12a73a0d0>
In [251]:
plt.imshow(story_img_arr[15])
Out[251]:
<matplotlib.image.AxesImage at 0x12a37cf70>
In [263]:
pickachu_after_attack = np.hstack([pickachu_teasing2, pickachu_teasing1])
plt.imshow(pickachu_after_attack)
Out[263]:
<matplotlib.image.AxesImage at 0x12a83c790>
In [264]:
pickachu_after_attack.shape
Out[264]:
(378, 996, 3)
In [273]:
scene_12 = add_whitespace_area(pickachu_after_attack, 100)
scene_12 = write_text(scene_12, 
                      50, 378, 
                      """Pickachu teases with funny faces!!"""
                      , 65)
plt.imshow(scene_12)
scene_12.save('out/scene12.jpg')
In [275]:
plt.imshow(story_img_arr[-2])
Out[275]:
<matplotlib.image.AxesImage at 0x12ac141c0>
In [277]:
story_img_arr[-2].shape
Out[277]:
(576, 500, 3)
In [281]:
scene_13 = add_whitespace_area(story_img_arr[-2], 100)
scene_13 = write_text(scene_13, 
                      10, 576, 
                      """I like this small creature, 
 he's brave and courageous"""
                      , 40)
plt.imshow(scene_13)
scene_13.save('out/scene13.jpg')
In [282]:
plt.imshow(story_img_arr[-1])
Out[282]:
<matplotlib.image.AxesImage at 0x12ae3aa90>
In [283]:
story_img_arr[-1].shape
Out[283]:
(225, 300, 3)
In [289]:
scene_14 = add_whitespace_area(story_img_arr[-1], 120)
scene_14 = write_text(scene_14, 
                      10, 225, 
                      """Pickachu on hearing 
these compliments 
also gives up anger"""
                      , 30)
plt.imshow(scene_14)
scene_14.save('out/scene_14.jpg')
In [ ]:
 
In [252]:
story_img_arr[15].shape
Out[252]:
(378, 498, 4)

Now Let's resize into same size¶

In [295]:
all_outputs = glob.glob('out/*')
In [296]:
all_outputs.sort()
In [297]:
all_outputs
Out[297]:
['out/1.jpg',
 'out/2.jpg',
 'out/3.jpg',
 'out/4.jpg',
 'out/5.jpg',
 'out/6.jpg',
 'out/7.jpg',
 'out/8.jpg',
 'out/9.jpg',
 'out/91.jpg',
 'out/92.jpg',
 'out/93.jpg',
 'out/94.jpg',
 'out/95.jpg']
In [317]:
fig = plt.figure(figsize=(10, 25))
columns = 2
rows = 7
for i in range(1, columns*rows +1):
    output_image = plt.imread(all_outputs[i-1])
    img = output_image
    fig.add_subplot(rows, columns, i)
    plt.title(f'Image {i}')
    plt.imshow(img)
fig1 = plt.gcf()
plt.show()
In [319]:
plt.draw()
fig1.savefig('foo.jpg', dpi=100)
<Figure size 432x288 with 0 Axes>
In [320]:
!pip freeze > requirements.txt
In [ ]: